<?php

namespace app\index\controller;

use addons\epay\library\Service;
use app\admin\model\Cpackage;
use app\admin\model\Order;
use app\admin\model\Package;
use app\admin\model\Pvideo;
use app\admin\model\Video;
use app\common\controller\Frontend;
use app\wxpay\controller\WxpayService;
use fast\Random;

/**
 * create by PhpStorm 2025/3/8 15:53
 * guopin.com app\index\controller@Course
 * 行止之行 <xingzhi8711@gmail.com>
 * 课程
 */
define('APPID', '2021001189645192');
//商户私钥
define('rsaPrivateKey', 'MIIEogIBAAKCAQEAmJ5jeaxqDCO+mU+yEVeCyo410C8JkPG9BgCkFX2DRGno7cqwaG95fFyCSLYRncUqKgJFd1kWiRoQ1gPb9AN3Ex4KSWFn9ebkpGoRXH8gXaLfXqMeA+2SF34z7qnzYslw+dLzo8ZFOEna7lPbU31L4opC6Q5Aa+X7/nAlv+W95AjoZY+d53fRud899ij4Bl4Sn05LD0U8X2Obi6+FvSWC9iLRzz9wZWOdXJYE4mhi8k1V+TE67GqT3R/Ykqafhz1Q39rC4c+JGuZ5VUR4Tes1ubhLsniLjVxPeo6g8T7Rder1UZGTSSKDkNLzgPgtCmx/hSnSOPSPGEWA04l79R79nQIDAQABAoIBAEJQyI6tMWNXANHjRE+uw+E6C/4LAlAeCnGaQ7Im2KwYjSMOaLrofVRhaicoHd19N8YLOJ4UcuteDSTBhoiwHMSM0BF0ptS94GnkwOh9QNYPGJoprLguOcXFi9FPvbcWKuYTGNT4KUm9JclbXLkZf6jPRSW5kJqR036KwoxwyqNT4yf6XaVQ1z1Cwe+lyvLidIh7X6tffgFOspT6wRgnAtS6mb+uowju77QzjWLH3TpOSfpdoo3dTfbn8WH4E/M9u1pcNwKU2An1MOpcKmsrWl5tuzwsIplP0Hg7BjOhB5d3jmuHZnm0dugI1+34yFPtA0RrsTBas+ESux6WV/Hwlq0CgYEA10a+bhssODMMEFKrfFxAWNJ7fl5nA6Kex3ftC8G4ks8y621HdLoNHmgcmP/wivILrPXwdaO40luxBCP5+HdH9rx/7vWIZlkWi0xG33G/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i8k1V+TE67GqT3R/Ykqafhz1Q39rC4c+JGuZ5VUR4Tes1ubhLsniLjVxPeo6g8T7Rder1UZGTSSKDkNLzgPgtCmx/hSnSOPSPGEWA04l79R79nQIDAQAB');
define('rsaPublicKey', 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkSlYUxuS/nhkkn+mIts/EFFhAN/LwWyOzNffwEdLNPrqZW48Z2DzlKNqf78F5CeHHviuec6xa4p8bnyNzhNl5KWWVgiSxapr8TAvlvBhB0bze6nzWfI45LDNPCwLbyrrEeemW83bA7ljQ6oIUrnhkMiPRCbkIlibzdtSdEXxUhrZ2QRppfNBEF6DbeLIBtlRcOOXgNhGiCQ3dKOoB4pPl2cSASZpzHkfRPBl2k1KZyQnLtz2eYiaRS5MdgcAweW9+34VGAjOCDGuCXtzjuJxyWd8JNCqOK3EJR1nZRkzSydZJ4qePfDJ0DBm/6PeYkdNG/bGjPtHTaNVtqDytEOsswIDAQAB');
//微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送
define('mchid_wx', '1600829842');
//公众号APPID 通过微信支付商户资料审核后邮件发送
//define('appid_wx','wx99a6c60cd13fd954');
define('appid_wx', 'wxcc09260e52be3147');
//微信支付申请对应的公众号的APP Key
//define('appKey_wx','5b7ce0312f39893d519ff43c76332641');e12d26c7ec2c156ec10d6ccdfdb007e8
define('appKey_wx', 'e12d26c7ec2c156ec10d6ccdfdb007e8');
//https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
define('apiKey_wx', 'c19475ff0d6dafee7ecc55c7a5d3a92d');
class Course extends Frontend
{
    protected $noNeedLogin = ['detail','st'];
    protected $noNeedRight = '*';
    public function _initialize()
    {
        parent::_initialize();
        $this->cateId=15;
        $this->assign('c',$this->cateId);
    }
    public function detail()
    {
        $id=input('c',0);
        $detail=\app\admin\model\Course::find($id);
        if(!$detail) $this->error('课程不存在');
        $this->assign("detail",$detail);
        $packages=\app\admin\model\Course::playTree($id);
        $this->assign("packages",$packages);
        $this->assign('title',$detail->title);
        return $this->fetch();
    }
    public function buy()
    {
        $id=input('c',0);
        $detail=\app\admin\model\Course::find($id);
        if(!$detail) $this->error('课程不存在');
        $this->assign("detail",$detail);
        $package=Cpackage::with(['package'])->where('course_id',$id)->select();
        $this->assign("package",$package);
        $this->assign('title',$detail->title);
        return $this->fetch();
    }

    public function pay()
    {
        header('Content-type:text/html; Charset=utf-8');
        $info=\app\admin\model\User::get($this->auth->id);
        $oid="O".date("Ymdhis").Random::alnum(8);
        $cid=request()->param('cid');//课程id
        $course=\app\admin\model\Course::get($cid);
        $title=request()->param('title');//课程简称
        $code=request()->param('code');
        $param=[
            'orderid'=>$oid,
            'amount'=>$course['price'],
            'type'=>$code,
            'title'=>$title,
            'notifyurl'=>$this->request->domain()."/index/notify/index/paytype/".$code,
            'returnurl'=>$this->request->domain()."/index/course/ok",
            'method'=>"web"
        ];
        $result=Service::submitOrder($param);
        $url=$result->getTargetUrl();
        Order::create([
            'order_no'=>$oid,
            'user_id'=>$info['id'],
            'pay_no'=>$oid,
            'course_id'=>$course['id'],
            'exit_time'=>time()+3600,
            'price'=>$course['price'],
            'status'=>'no_pay',
        ]);
        header('location:'.$url);

    }
    public function st()
    {
        $id = $this->request->param('cid');
        $detail = \app\admin\model\Course::get($id);
        $st_id = $detail->st_id;
        if (!$st_id) $this->error('视频不存在');
        $st_ids = explode('-', $st_id);
        if (!isset($st_ids[1])) $this->error('视频不存在');
        $video = Video::find($st_ids[1]);
        $this->assign('video', $video);
        return $this->fetch();

    }
}